package com.comtime.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.AMap.InfoWindowAdapter;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.comtime.greendaodemo.OpenLockInfo;
import com.comtime.greendaodemo.RemindInfo;
import com.comtime.service.MyService;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.MainActivity;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.ToastUtils;
import com.comtime.utils.Util;
import com.comtime.view.MyDialog;

public class MapActivity extends BaseActivity implements InfoWindowAdapter {
	private MapView mapView;
	MarkerOptions myLocationOverlay;
	ImageView image_nonetwork;
	TextView text_losttime;
	private AMap aMap;
	Marker mMarker = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		Intent getIntent=getIntent();
		
		int value_tag=getIntent.getIntExtra(MyService.VALUE_TAG,0);
        String valuee = getIntent.getStringExtra(MyService.VALUE_INFO);
        Log.e("RZC","VALUE_INFO= "+valuee);
		if(value_tag==0){
			OpenLockInfo info= (OpenLockInfo) getIntent.getSerializableExtra(MyService.VALUE_INFO);
			latitude=info.getLat();
			longtitude=info.getLng();
			lostTime=info.getTime();
			address=info.getAddress();
		}else
			if(value_tag==1){
			//断开提醒记录
			RemindInfo info=(RemindInfo) getIntent.getSerializableExtra(MyService.VALUE_INFO);
			latitude=info.getLat();
			longtitude=info.getLng();
			lostTime=info.getTime();
			address=info.getAddress();
		}
		
		
		mapView = (MapView) findViewById(R.id.show_map);
		mapView.onCreate(savedInstanceState);// 必须要写
		image_nonetwork = (ImageView) findViewById(R.id.image_nonetwork_lostlocation);
		text_losttime = (TextView) findViewById(R.id.text_losttime);


		initMap();
		showLocationAddress();
	}

	public void BackAction(View v){
		this.finish();
		overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
	}
	private void initMap() {
		if (aMap == null) {
			aMap = mapView.getMap();
			aMap.setInfoWindowAdapter(this);//
			// showInfoWindow();

			aMap.moveCamera(CameraUpdateFactory.zoomTo(4));
		}
		myLocationOverlay = new MarkerOptions().icon(BitmapDescriptorFactory
				.fromResource(R.drawable.icon_map0));
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				mMarker = aMap.addMarker(myLocationOverlay.position(myPoint)
						.title(lostTime).snippet(address));
				mMarker.showInfoWindow();
				break;

			default:
				break;
			}
		}

	};
	String lostTime = "2016-06-24 03:33";
	String address = "上海市徐汇区宜山路515号";
	private double latitude=31.1801789867;
	private double longtitude=121.4256078849;
	CameraUpdate update;
	LatLng myPoint;

	void showLocationAddress() {
		if (lostTime != null && !lostTime.equals("")) {
			Log.e("RZC","NetWork"+Util.hasNetwork(this));
			Log.e("RZC","2address"+address);

			if (address != null && !address.equals("") && Util.hasNetwork(this)) {
				image_nonetwork.setVisibility(View.GONE);
				text_losttime.setVisibility(View.GONE);

				Log.i("tag", "location-丢失地址：" + address);
				if (latitude != -1 && longtitude != -1) {

					myPoint = new LatLng(latitude, longtitude);
					update = CameraUpdateFactory.newLatLngZoom(myPoint, 16);
					aMap.animateCamera(update);

					mMarker = aMap.addMarker(myLocationOverlay
							.position(myPoint));

					mMarker.showInfoWindow();

					handler.postDelayed(new Runnable() {// // 避免
														// marker第一次显示失败，500ms后再次尝试

								@Override
								public void run() {
									handler.sendEmptyMessage(1);
								}
							}, 500);

				}
			} else {
				image_nonetwork.setVisibility(View.GONE);
				text_losttime.setVisibility(View.GONE);
				image_nonetwork.setVisibility(View.VISIBLE);
				//image_nonetwork.setImageResource(R.drawable.ic_launcher);
				text_losttime.setVisibility(View.VISIBLE);
				text_losttime.setText(getText(R.string.time) + ":" + lostTime);
				ToastUtils.showToast(this,"无法连接网络，不能显示位置信息",1500);
			}
		} else {
			image_nonetwork.setVisibility(View.INVISIBLE);
			text_losttime.setVisibility(View.INVISIBLE);
			aMap.moveCamera(CameraUpdateFactory.zoomTo(4));
			showDialong();

		}
	}

	private void showDialong() {
		final MyDialog dialog = new MyDialog(this);

		dialog.setContent(R.string.no_lostlocation);
		dialog.setLeftButtonText(getText(R.string.ok));
		dialog.getLeftButton().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	@Override
	public View getInfoContents(Marker arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public View getInfoWindow(Marker arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}

}
